package ControllerSrevlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MYJDBC.jdbc;

/**
 * Servlet implementation class SeedMsg
 */
@WebServlet("/SeedMsg")
public class SeedMsg extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeedMsg() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String msgtext = request.getParameter("msgtext");
		String userid = request.getParameter("userid");
		String fid = request.getParameter("fid");
		String msgto = request.getParameter("msgto");
		String msgin = request.getParameter("msgin");
		String usersimg = request.getParameter("usersimg");
		
		int Buserid = Integer.parseInt(userid);
		int Bfid = Integer.parseInt(fid);
		int Bmsgto = Integer.parseInt(msgto);
		int Bmsgin = Integer.parseInt(msgin);
		Date date = new Date();//Date是java.util.Date
		Timestamp time = new Timestamp(date.getTime());
		int a = 1;int b = 0;

		jdbc jc = new jdbc();
		try {
			jc.SqlUpdate("insert into user_msg (user_id,user_myfid,msg_text,msg_in,msg_to,user_simg,msg_log) value ("+Buserid+","+Bfid+",'"+msgtext+"',"+Bmsgin+","+Bmsgto+",'"+usersimg+"',"+b+")");
			jc.SqlUpdate("insert into user_msg (user_id,user_myfid,msg_text,msg_in,msg_to,user_simg,msg_log) value ("+Bfid+","+Buserid+",'"+msgtext+"',"+Bmsgto+","+Bmsgin+",'"+usersimg+"',"+a+")");
			jc.UpdateClose();
			response.sendRedirect("BlueVideoUserMsgFriend?fid="+fid+"&userid="+Buserid+"");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}

}
